import request from '@/config/axios'
import type { WorkControlData, WorkData } from './types'

export const getWorkListApi = (params: any): Promise<IResponse> => {
    const queryParams = params.queryParams ? params.queryParams : {}
    if (params.queryParams) {
        delete params.queryParams
    }
    Object.assign(params, queryParams)
    return request.get({ url: '/work/', params })
}

export const saveOrUpdate = (data: Partial<WorkData>, isUpdate = false): Promise<IResponse> => {
    if (isUpdate) {
        return request.put({ url: '/work/', data })
    } else {
        return request.post({ url: '/work/', data })
    }
}

export const getWorkDetailApi = (id: string | number, companyId: string | number): Promise<IResponse<WorkData>> => {
    return request.get({ url: '/work/info', params: { id, companyId } })
}

export const delWorkApi = (data: Partial<WorkData>[]): Promise<IResponse> => {
    if (data.length > 0) {
        const ids = data.map((d) => d.user_id);
        return request.delete({ url: `/work/delete/${ids.join(',')}` })
    } else {
        const id = data[0].user_id
        const company_id = data[0].company_id
        return request.delete({ url: `/work/${id}/${company_id}` })
    }
}

export const getWorkControlApi = (user_id: string | number, company_id: string | number): Promise<IResponse<WorkControlData>> => {
    return request.get({ url: '/user/control/', params: {user_id, company_id} })
}
export const saveWorkControlApi = (data: Partial<WorkControlData>): Promise<IResponse> => {
    return request.post({ url: '/user/control/', data })
}
